﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HTMapLib;
using HTMapLib.Classes;
using MapControl.Misc;
using MapControl.Misc.DAL;

namespace GPSMap
{
    public partial class frmAirport : Form
    {
        private Airport _objAirport;
        private AirPortRepository _airPortRespository;
        private frmAirportList frmParent;

        public frmAirport(frmAirportList frm)
        {
            InitializeComponent();
            frmParent = frm;
        }


        public frmAirport(frmAirportList frm, Airport objAirport)
        {
            InitializeComponent();
            _objAirport = objAirport;
            frmParent = frm;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                string airport_name = txtAirport.Text.Trim();
                if (_airPortRespository.IsExist(airport_name))
                {
                    MessageBox.Show("Tên sân bay đã tồn tại");
                    return;
                }

                if (radioButtonDMS.Checked)
                {
                    double lonD = double.Parse(txtLonD.Text);
                    double lonM = double.Parse(txtLonM.Text);
                    double lonS = double.Parse(txtLonS.Text);
                    double lon = HTCollectionHelper.DMSToDecimal("E", lonD, lonM, lonS);
                    txtLongitutde.Text = lon.ToString("#.0000000");

                    double latD = double.Parse(txtLatD.Text);
                    double latM = double.Parse(txtLatM.Text);
                    double latS = double.Parse(txtLatS.Text);
                    double lat = HTCollectionHelper.DMSToDecimal("N", latD, latM, latS);
                    txtLatitude.Text = lat.ToString("#.0000000");
                }

                if (_objAirport != null)
                {
                    _objAirport.Airport_Name = txtAirport.Text;
                    _objAirport.Longitude = double.Parse(txtLongitutde.Text);
                    _objAirport.Latitude = double.Parse(txtLatitude.Text);
                    _airPortRespository.Update(_objAirport);
                    frmParent.RefreshDataGridView(false);
                    frmParent.isChanged = true;
                }
                else
                {
                    _objAirport = new Airport();
                    _objAirport.Airport_Name = txtAirport.Text;
                    _objAirport.Longitude = double.Parse(txtLongitutde.Text);
                    _objAirport.Latitude = double.Parse(txtLatitude.Text);
                    _airPortRespository.Insert(_objAirport);
                    frmParent.RefreshDataGridView(true);
                    frmParent.isChanged = true;
                }
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmAirport_Load(object sender, EventArgs e)
        {
            _airPortRespository = new AirPortRepository(GeneralClass.appDBFile);
            radioButtonDecimal.Checked = true;
            if (_objAirport != null)
            {
                txtAirport.Text = _objAirport.Airport_Name;
                txtLongitutde.Text = _objAirport.Longitude.ToString("#.0000000");
                txtLatitude.Text = _objAirport.Latitude.ToString("#.0000000");
            }
            
        }

        private void ConvertToDecimal()
        {
            if (_objAirport != null)
            {
                if (_objAirport.Longitude > 0)
                {
                    txtLongitutde.Text = _objAirport.Longitude.ToString("#.0000000");
                    txtLatitude.Text = _objAirport.Latitude.ToString("#.0000000");
                }
            }
        }

        private void ConvertToDMS()
        {
            if (_objAirport != null)
            {
                string lonD, lonM, lonS, latD, latM, latS;
                lonD = lonM = lonS = latD = latM = latS = "";
                if (_objAirport.Longitude > 0)
                {
                    HTCollectionHelper.DecimalToDMS(_objAirport.Longitude, ref lonD, ref lonM, ref lonS);
                    txtLonD.Text = lonD;
                    txtLonM.Text = lonM;
                    txtLonS.Text = lonS;

                    HTCollectionHelper.DecimalToDMS(_objAirport.Latitude, ref latD, ref latM, ref latS);
                    txtLatD.Text = latD;
                    txtLatM.Text = latM;
                    txtLatS.Text = latS;
                }
            }
        }

        private void radioButtonThapPhan_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonDecimal.Checked)
            {
                panelThapPhan.Visible = true;
                panelDoPhutGiay.Visible = false;
                ConvertToDecimal();
            }
        }

        private void radioButtonDoPhutGiay_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonDMS.Checked)
            {
                panelThapPhan.Visible = false;
                panelDoPhutGiay.Visible = true;
                panelDoPhutGiay.Location = panelThapPhan.Location;
                ConvertToDMS();
            }
        }

        public void GetSelectedCoord(double lon, double lat)
        {
            txtLongitutde.Text = lon.ToString("#.0000000");
            txtLatitude.Text = lat.ToString("#.0000000");
            radioButtonThapPhan_CheckedChanged(null, null);
        }

        private void btnSelectCoord_Click(object sender, EventArgs e)
        {
            frmParent.frmParent.LoadAirportForm(this);
        }
    }
}
